//
//  XNewVersionAlertView.m
//  ReceiveVouchers
//
//  Created by admin on 2017/7/6.
//  Copyright © 2017年 LiX i n long. All rights reserved.
//

#import "VRChooseDateBaseView.h"

#import "UIImage+VR.h"


@interface VRChooseDateBaseView()

//title
@property (nonatomic,retain) UILabel *titleLbl;
//内容
@property (nonatomic,retain) UILabel *msgLbl;
//横线线
@property (nonatomic,retain) UIView *lineView;
//竖线
@property (nonatomic,retain) UIView *vLineView;
// 背景图
@property (nonatomic, strong) UIImageView *backgrounImageView;

@property (nonatomic, weak)  UIDatePicker *datePicker;

//确认按钮
@property (nonatomic,retain) UIButton *sureBtn;
//取消按钮
@property (nonatomic,retain) UIButton *cancleBtn;


@end

@implementation VRChooseDateBaseView

- (instancetype)init
{
    if (self = [super init]) {
        
        self.isHidenAlertBySure = YES;
        
        self.frame = [UIScreen mainScreen].bounds;
        
        self.backgroundColor = RGBA(0, 0, 0, 0);
        
        self.alertView = [[UIView alloc] init];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 250);
        
        self.titleLbl = [[UILabel alloc] init];
        self.titleLbl.text = @"选择月份";
        self.titleLbl.textColor = RGB(51, 51, 51);
        self.titleLbl.textAlignment = NSTextAlignmentCenter;
        self.titleLbl.font = VRFont(18);
        [self.alertView addSubview:self.titleLbl];
        [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.alertView);
            make.centerY.mas_equalTo(self.alertView.mas_top).offset(20);
        }];
        
        // 确定按钮
        self.sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.sureBtn.tag = 101;
        [self.sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [self.sureBtn setTitleColor:RGB(64, 64, 64) forState:UIControlStateNormal];
        self.sureBtn.titleLabel.font = VRFont(16);
        self.sureBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.alertView addSubview:self.sureBtn];
        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-24);
            make.centerY.mas_equalTo(self.titleLbl);
            make.width.mas_equalTo(44);
            make.height.mas_equalTo(20);
        }];
        
        // 取消按钮
        self.cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.cancleBtn.tag = 100;
        [self.cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [self.cancleBtn setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
        self.cancleBtn.titleLabel.font = VRFont(16);
        self.cancleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.alertView addSubview:self.cancleBtn];
        [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(24);
            make.centerY.mas_equalTo(self.titleLbl);
            make.width.mas_equalTo(44);
            make.height.mas_equalTo(20);
        }];
        
        // 横线
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = RGB(238, 238, 238);
        [self.alertView addSubview:self.lineView];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.alertView);
            make.top.mas_equalTo(40);
            make.height.mas_equalTo(0.5);
        }];
        
//        UIDatePicker *datePicker = [[UIDatePicker alloc] init];
//        datePicker.datePickerMode = UIDatePickerModeDate;
//        [self.alertView addSubview:datePicker];
//        self.datePicker = datePicker;
//        [datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.mas_equalTo(self.alertView);
//            make.top.mas_equalTo(60);
//            make.width.mas_equalTo(280);
//        }];
        
        [self addSubview:self.alertView];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
        [self addGestureRecognizer:tapGesture];
    }
    
    return self;
}


#pragma mark - 弹出 -
- (void)showOnWindow
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [rootWindow bringSubviewToFront:self];
    [self creatShowAnimation];
}

- (void)showOnView:(UIView *)view
{
    [view addSubview:self];
    [self creatShowAnimation];
}

- (void)dissmiss
{
    [UIView animateWithDuration:0.2 animations:^{
        
        self.alertView.y = kScreenHeight - self.topMargin;
        self.backgroundColor = RGBA(0, 0, 0, 0);
    } completion:^(BOOL finished) {
        self.hidden = YES;
        [self removeFromSuperview];
    }];
}

- (void)creatShowAnimation
{
    self.hidden = NO;
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView animateWithDuration:0.2 animations:^{
        
        self.alertView.y = kScreenHeight - self.alertView.height - self.topMargin;
        self.backgroundColor = RGBA(0, 0, 0, 0.4);
    } completion:^(BOOL finished) {
        
    }];
}

// 弹性动画
- (void)showOscillatoryAnimationWithLayer:(CALayer *)layer type:(BOOL)type{
    NSNumber *animationScale1 = type ? @(1.15) : @(0.5);
    NSNumber *animationScale2 = type ? @(0.92) : @(1.15);
    
    [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
        [layer setValue:animationScale1 forKeyPath:@"transform.scale"];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
            [layer setValue:animationScale2 forKeyPath:@"transform.scale"];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                [layer setValue:@(1.0) forKeyPath:@"transform.scale"];
            } completion:nil];
        }];
    }];
}


#pragma mark - action
- (void)buttonEvent:(UIButton *)sender
{
    if (self.ActionBlock) {
        self.ActionBlock(sender.tag - 100);
    }

    if (sender.tag == 100 || self.isHidenAlertBySure) {
        [self dissmiss];
    }
}

- (void)handleTapGesture:(UITapGestureRecognizer *)recongnizer
{
    [self dissmiss];
}

#pragma mark - setter && getter
- (void)setTopMargin:(CGFloat)topMargin
{
    _topMargin = topMargin;
    
    self.y = topMargin;
}

@end
